Java Dragging an object from one area to another [on hold]
Posted
by
user50369
on Game Development
See other posts from Game Development
or by user50369
Published on 2014-08-24T15:19:48Z
Indexed on
2014/08/24
16:29 UTC
Read the original article
Hit count: 229
java
Hello I have a game where you drag bits of food around the screen. I want to be able to click on an ingredient and drag it to another part of the screen where I release the mouse. I am new to java so I do not really know how to do this please help me Here is me code. This is the class with the mouse listeners in it:
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
Comp.ml = true;
// placing
if (manager.title == true) {
if (title.r.contains(Comp.mx, Comp.my)) {
title.overview = true;
} else if (title.r1.contains(Comp.mx, Comp.my)) {
title.options = true;
} else if (title.r2.contains(Comp.mx, Comp.my)) {
System.exit(0);
}
}
if (manager.option == true) {
optionsMouse(e);
}
mouseinventory(e);
} else if (e.getButton() == MouseEvent.BUTTON3) {
Comp.mr = true;
}
}
private void mouseinventory(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
} else if (e.getButton() == MouseEvent.BUTTON1) {
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
Comp.ml = false;
} else if (e.getButton() == MouseEvent.BUTTON3) {
Comp.mr = false;
}
}
@Override
public void mouseDragged(MouseEvent e) {
for(int i = 0; i < overview.im.ing.toArray().length; i ++){
if(overview.im.ing.get(i).r.contains(Comp.mx,Comp.my)){
overview.im.ing.get(i).newx = Comp.mx;
overview.im.ing.get(i).newy = Comp.my;
overview.im.ing.get(i).dragged = true;
}else{
overview.im.ing.get(i).dragged = false;
}
}
}
@Override
public void mouseMoved(MouseEvent e) {
Comp.mx = e.getX();
Comp.my = e.getY();
// System.out.println("" + Comp.my);
}
This is the class called ingredient
public abstract class Ingrediant {
public int x,y,id,lastx,lasty,newx,newy;
public boolean removed = false,dragged = false;
public int width;
public int height;
public Rectangle r = new Rectangle(x,y,width,height);
public Ingrediant(){
r = new Rectangle(x,y,width,height);
}
public abstract void tick();
public abstract void render(Graphics g);
}
and this is a class which extends ingredient called hagleave
public class HagLeave extends Ingrediant {
private Image img;
public HagLeave(int x, int y, int id) {
this.x = x;
this.y = y;
this.newx = x;
this.newy = y;
this.id = id;
width = 75;
height = 75;
r = new Rectangle(x,y,width,height);
}
public void tick() {
r = new Rectangle(x,y,width,height);
if(!dragged){
x = newx;
y = newy;
}
}
public void render(Graphics g) {
ImageIcon i2 = new ImageIcon("res/ingrediants/hagleave.png");
img = i2.getImage();
g.drawImage(img, x, y, null);
g.setColor(Color.red);
g.drawRect(r.x, r.y, r.width, r.height);
}
}
The arraylist is in a class called ingrediantManager:
public class IngrediantsManager {
public ArrayList<Ingrediant> ing = new ArrayList<Ingrediant>();
public IngrediantsManager(){
ing.add(new HagLeave(100,200,1));
ing.add(new PigHair(70,300,2));
ing.add(new GiantsToe(100,400,3));
}
public void tick(){
for(int i = 0; i < ing.toArray().length; i ++){
ing.get(i).tick();
if(ing.get(i).removed){
ing.remove(i);
i--;
}
}
}
public void render(Graphics g){
for(int i = 0; i < ing.toArray().length; i ++){
ing.get(i).render(g);
}
}
}
© Game Development or respective owner